let App = getApp();
var i;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopCate:[],//商店分类
    shopCate_id:'',
    shopCate_name:'',
    dist: [],//区信息
    dist_id:'',
    dist_name:'',
    shoppingMall:[],//商城集合
    shoppingMall_id:'',
    shoppingMall_name:'',


    disabled: false,
    nav_select: false, // 快捷导航
    name: '',
    region: '',
    detail: '',
    wxapp_id:'',
    error: '',
    tempFilePaths:[],
    showImages: [],
    images:[],
    img_num:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  onShow: function () {
    i = 0;
    // 获取商店分类
    this.getShopCate();
    this.getDistrict();
    
  },
  //获取商店分类
  getShopCate: function () {
    let _this = this;
    App._get('shop/getShopCate', {}, function (result) {
      //console.log(result);
      _this.setData({
        shopCate: result.data.shopCate
      });
    });
  },
  //选择商品分类 
  bindShopCateChange: function (e) {
    let _this = this;
    this.setData({
      shopCate_id: _this.data.shopCate[e.detail.value].cate_id,
      shopCate_name: _this.data.shopCate[e.detail.value].name
    })
  },

  //获取区信息
  getDistrict: function () {
    let _this = this;
    // let user_address_id = this.data.shoppingMallId
    // let shopping_mall_city = this.data.shopping_mall_city;
    // let shopping_mall_district = this.data.shopping_mall_district;
    App._get('shop/getShopMallDist', {
    }, function (result) {
      _this.setData({
        dist: result.data.list
      });
    });
  },

  bindDistChange:function(e){
    let _this = this;
    _this.setData({
      dist_id: _this.data.dist[e.detail.value].id,
      dist_name: _this.data.dist[e.detail.value].title
    });

    //通过区id 获取商城集合
    App._get('shop/getShoppingMall', {
      dist_id: _this.data.dist[e.detail.value].id
    }, function (result) {
      //console.log(result);
      _this.setData({
        shoppingMall: result.data.list
      });
    });
  },

  //商城选择
  bindShoppingMallChange:function(e){
    let _this = this;
    _this.setData({
      shoppingMall_id: _this.data.shoppingMall[e.detail.value].shopping_mall_id,
      shoppingMall_name: _this.data.shoppingMall[e.detail.value].shopping_mall_cont
    });
  },

  /**
   * 表单提交
   */
  saveData: function(e) {
    let _this = this,
    values = e.detail.value;
    values.cate_id = this.data.shopCate_id;//商店分类id
    values.dist_id = this.data.dist_id;//区id
    values.shoppingMall_id = this.data.shoppingMall_id;//商城id
    values.images = this.data.images;
    // 记录formId
    //App.saveFormId(e.detail.formId);

    //表单验证
    if (!_this.validation(values)) {
      App.showError(_this.data.error);
      return false;
    }

    // 按钮禁用
    _this.setData({
      disabled: true
    });
    //提交到后端
    App._post_form('shop/addShop', values, function(result) {
      App.showSuccess(result.msg, function() {
        wx.navigateBack();
      });
      //console.log(result);
    }, false, function() {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },
  
  

  /**
   * 表单验证
   */
  validation: function(values) {
    
    if (values.cate_id === '') {
      this.data.error = '请选择商店分类';
      return false;
    }

    if (values.dist_id === '') {
      this.data.error = '请选择城区';
      return false;
    }
    
    if (values.shoppingMall_id === '') {
      this.data.error = '请选择商城';
      return false;
    }
    if (values.address === '') {
      this.data.error = '详细地址不能为空';
      return false;
    }
    if (values.shop_name === '') {
      this.data.error = '商店名称不能为空';
      return false;
    }
    if (values.phone === '') {
      this.data.error = '商店电话不能为空';
      return false;
    }

    if (values.phone.length != 11) {
      this.data.error = '请上传11位号码';
      return false;
    }
    // 验证手机号
    let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
    if (!reg.test(values.phone)) {
      this.data.error = '手机号不符合要求';
      return false;
    }
    
    
    if (values.description === '') {
      this.data.error = '商店简介不能为空';
      return false;
    }

    if (this.data.images.length < 1) {
      this.data.error = '请上传至少一张图片';
      return false;
    }

    return true;
  },

  bindlongpressimg: function (e) {
    let that = this;
    let key = e.target.dataset.id;
    let imagesArr = that.data.images;
    let showImagesArr = that.data.showImages;

    imagesArr.splice(key, 1);
    showImagesArr.splice(key, 1);
    that.setData({
      images: imagesArr,
      showImages: showImagesArr
    })

    console.log(that.data.images);

  },

  //查看图片
  previewImage: function (e) {
    let key = e.target.dataset.id;
    let imgSrc = this.data.showImages;

    wx.previewImage({
      current: imgSrc[key],
      urls: imgSrc
    })
  },

  //发起图片提交
  img_item: function (e) {
    var that = this;
    if (that.data.showImages.length > 2) {
      App.showError('只可上传2张图片', function () {
      });
      return false;
    }

    wx.chooseImage({
      count: 2,
      sizeType: ['original'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        if ((that.data.showImages.length + res.tempFilePaths.length) > 2) {
          App.showError('只可上传2张图片', function () {
          });
          return false;
        }
        that.setData({
          'tempFilePaths': ''
        })
        i = 0;
        that.setData({
          'tempFilePaths': res.tempFilePaths
        })
        
        that.btn_up();
      }
    })
  },

  //提交图片
  btn_up: function (e) {
    var that = this;
    let token = wx.getStorageSync('token');
    wx.showLoading({
      title: '图片上传中...',
    })
    console.log(that.data.tempFilePaths);
    wx.uploadFile({
      url: App.api_root + 'address/addImage', // 仅为示例，非真实的接口地址
      filePath: that.data.tempFilePaths[i],
      name: 'file',
      formData: {
        wxapp_id: App.siteInfo.uniacid,
        token: token
      },
      success(res) {
        console.log(res);
        let arr = JSON.parse(res.data);
        if (arr.code == 1) {
          let arr2 = JSON.parse(arr.data);
          var showImagesArr = that.data.showImages;
          showImagesArr.push(App.root + arr2.url);
          var imagesArr = that.data.images;
          imagesArr.push(arr2.url);
          that.setData({
            'showImages': showImagesArr,
            'images': imagesArr
          })
          i++;
          if (i < that.data.tempFilePaths.length) {
            that.btn_up();
          }
        } else {
          App.showError(arr.msg);
        }
        wx.hideLoading();
      }
    })
  },

})